package org.iteam.xlz.manager.action.ticket;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.iteam.web.shiro.ShiroUtil;
import org.iteam.web.struts.action.AbstractModuleAction;
import org.iteam.web.struts.action.RestAction;
import org.iteam.xlz.core.entity.ticket.TicketInfo;
import org.iteam.xlz.core.service.base.TickettypeService;
import org.iteam.xlz.core.service.product.ProductSaleService;
import org.iteam.xlz.core.service.ticket.TicketInfoService;

@SuppressWarnings("unchecked")
@Results({@Result(name="INDEX",type="freemarker",location="/WEB-INF/content/ticket/info/index.html"),
		  @Result(name="EDIT",type="freemarker",location="/WEB-INF/content/ticket/info/edit.html")})
public class TicketInfoAction extends AbstractModuleAction<TicketInfo> implements RestAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6865321113163782333L;
	private final String PATH="/ticket/info";
	@Override
	public TicketInfo getModel() {
		if(null==model){
			model=new TicketInfo();
		}
		return model;
	}	
	
	@Override
	@Action(PATH+"/index")
	public String index() {
		map.put("typeTree",TickettypeService.getInstance().buildTicketTree());
		return INDEX;
	}

	@Override
	@Action(PATH+"/query")
	public String query() {
		map.put("total",TicketInfoService.getInstance().counts(name,productname,typeid,leftTime,rightTime));
		map.put("rows",TicketInfoService.getInstance().query(name,productname,typeid,leftTime,rightTime,page,rows));
		return MAP;
	}

	@Override
	@Action(PATH+"/create")
	public String create() {
		model.setCreator(ShiroUtil.currentAccount());
		boolean flag=TicketInfoService.getInstance().create(model);
		
		map.put("state",flag);
		if(!flag){
			map.put("msg","操作失败，请检查是否选择分类与商品。");
		}
		
		return MAP;
	}

	@Override
	@Action(PATH+"/update")
	public String update() {
		boolean flag=TicketInfoService.getInstance().update(model);
		map.put("state",flag);
		if(!flag){
			map.put("msg","操作失败，请检查是否选择分类与商品。");
		}
		return MAP;
	}

	@Override
	@Action(PATH+"/{_id}/delete")
	public String delete() {
		map.put("state",TicketInfoService.getInstance().delete(_id));
		return MAP;
	}

	@Override
	@Action(PATH+"/getProducts")
	public String destroy() {
		map.put("products",ProductSaleService.getInstance().getUnTicketProduct(model.getTickettype()));
		return MAP;
	}

	@Override
	@Action(PATH+"/{_id}/edit")
	public String edit() {
		map.put("typeTree",TickettypeService.getInstance().buildTicketTree());
		map.put("typeparent",TickettypeService.getInstance().getTypeParent(_id));
		object=TicketInfoService.getInstance().findById(_id);
		return EDIT;
	}

	@Override
	@Action(PATH+"/new")
	public String editNew() {
		map.put("typeTree",TickettypeService.getInstance().buildTicketTree());
		map.put("typeparent",TickettypeService.getInstance().getTypeParent(null));
		return EDIT;
	}

	@Override
	public String show() {
		return null;
	}	
	private String name;
	private String productname;
	private String typeid;
	private String leftTime;
	private String rightTime;

	public void setName(String name) {
		this.name = name;
	}

	public void setProductname(String productname) {
		this.productname = productname;
	}

	public void setTypeid(String typeid) {
		this.typeid = typeid;
	}

	public void setLeftTime(String leftTime) {
		this.leftTime = leftTime;
	}

	public void setRightTime(String rightTime) {
		this.rightTime = rightTime;
	}
}

